// Tags:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>

namespace utl {

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

inline int min(int a, int b) { return a < b ? a : b; }

}  // namespace utl
using utl::read;
using namespace std;

const int N = 2e5 + 5, inf = 0x3f3f3f3f;
int n, m, e_cnt, heads[N], fa[N];
bool cir[N];

struct Edge {
  int u, v, w;
} e[N];

inline void add(int u, int v, int w) {
  e[++e_cnt].u = u, e[e_cnt].v = v, e[e_cnt].w = w;
}

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("pair.in", "r", stdin);
  freopen("pair.out", "w", stdout);
#endif
#endif

  read(n), read(m);
  for (int i = 1, x, y, w; i <= m; ++i) {
    read(x), read(y), read(w);
    add(x, y, w);
  }
  for (int i = 1; i <= n; ++i) fa[i] = i;
  std::sort(e + 1, e + m + 1,
            [](const Edge &lhs, const Edge &rhs) { return lhs.w > rhs.w; });
  int ans = 0;
  for (int i = 1; i <= m; ++i) {
    if (find(e[i].u) != find(e[i].v) &&
        ((!cir[find(e[i].v)]) || (!cir[find(e[i].u)]))) {
      cir[find(e[i].v)] |= cir[find(e[i].u)];
      fa[find(e[i].u)] = find(e[i].v);
      ans += e[i].w;
    } else if (!cir[find(e[i].u)]) {
      cir[find(e[i].u)] = true;
      ans += e[i].w;
    }
  }
  printf("%d", ans);
  return 0;
}